import {fishCategory, getFishNames} from '../../util/fish'
Component({

    /**
     * 组件的属性列表
     */
    properties: {
      list: {
        type: Array,
        value: []
      },
    },

    lifetimes: {
      attached: function() {
        this.setData({
          selectList: JSON.parse(JSON.stringify(this.data.list)) 
        })
      },
      detached: function() {
        // 在组件实例被从页面节点树移除时执行
      },
    },

    /**
     * 组件的初始数据
     */
    data: {
      fishList: fishCategory,
      visible: false,
      selectList: []
    },

    /**
     * 组件的方法列表
     */
    methods: {
      open(){
        this.setData({
          visible: true,
          selectList: JSON.parse(JSON.stringify(this.data.list)) 
        })
      },
      onVisibleChange(e) {
        this.setData({
          visible: e.detail.visible,
        });
      },
      select(e){
      
        let id = e.currentTarget.dataset.item.value
        let index = -1
        for(let i = 0; i < this.data.selectList.length; i++){
          
          if(this.data.selectList[i] == id){
            index = i
          }
        }
        if(index == -1){
          this.data.selectList.push(id)
        } else {
          this.data.selectList.splice(index, 1)
        }
        
        this.setData({
          selectList: this.data.selectList
        })
      },
      confirm(){
        this.triggerEvent('confirm', this.data.selectList)

        this.cancel()
      },
      cancel(){
        this.setData({
          visible: false
        })
      }
    }
})